package io.jpress.commons.ip2region;

import io.jboot.utils.StrUtil;
import org.lionsoul.ip2region.DataBlock;

import java.io.Serializable;
import java.util.Arrays;
import java.util.regex.Pattern;

public class IpInfo implements Serializable {

    private static final Pattern SPLIT_PATTERN = Pattern.compile("\\|");

    /**
     * 城市id
     */
    private Integer cityId;
    /**
     * 国家
     */
    private String country;
    /**
     * 区域
     */
    private String region;
    /**
     * 省
     */
    private String province;
    /**
     * 城市
     */
    private String city;
    /**
     * 运营商
     */
    private String isp;

    private int dataPtr;


    public IpInfo() {
    }


    public IpInfo(DataBlock dataBlock) {
        if (dataBlock == null) {
            return;
        }

        int cityId = dataBlock.getCityId();
        setCityId(cityId == 0 ? null : cityId);
        setDataPtr(dataBlock.getDataPtr());


        String[] regionArray = SPLIT_PATTERN.split(dataBlock.getRegion());
        // 补齐5位
        if (regionArray.length < 5) {
            regionArray = Arrays.copyOf(regionArray, 5);
        }
        setCountry(filterZero(regionArray[0]));
        setRegion(filterZero(regionArray[1]));
        setProvince(filterZero(regionArray[2]));
        setCity(filterZero(regionArray[3]));
        setIsp(filterZero(regionArray[4]));
    }


    private static String filterZero(String info) {
        if (info == null || "0".equals(info)) {
            return null;
        }
        return info;
    }


    public Integer getCityId() {
        return cityId;
    }

    public void setCityId(Integer cityId) {
        this.cityId = cityId;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getIsp() {
        return isp;
    }

    public void setIsp(String isp) {
        this.isp = isp;
    }

    public int getDataPtr() {
        return dataPtr;
    }

    public void setDataPtr(int dataPtr) {
        this.dataPtr = dataPtr;
    }

    public String getAddressText(){
        return StrUtil.obtainDefault(getCountry(),"")
                + StrUtil.obtainDefault(getProvince(),"")
                + StrUtil.obtainDefault(getCity(),"");
    }

    @Override
    public String toString() {
        return "IpInfo{" +
                "cityId=" + cityId +
                ", country='" + country + '\'' +
                ", region='" + region + '\'' +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", isp='" + isp + '\'' +
                ", dataPtr=" + dataPtr +
                '}';
    }
}
